//
//  UIScrollView+RACSignal.m
//  高仿暴走斗图
//
//  Created by key on 16/5/4.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIScrollView+RACSignal.h"
#import "NSObject+RACDescription.h"

@implementation UIScrollView (RACSignal)

static void RACUseDelegateProxy(UIScrollView *self) {

    if (self.delegate == (id)self.rac_delegateProxy) return;
    self.rac_delegateProxy.rac_proxiedDelegate = self.delegate;
    self.delegate = (id)self.rac_delegateProxy;
}

- (RACDelegateProxy *)rac_delegateProxy {
    
    RACDelegateProxy *proxy = objc_getAssociatedObject(self, _cmd);
    if (proxy == nil) {
        proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UIScrollViewDelegate)];
        objc_setAssociatedObject(self, _cmd, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return proxy;
}


- (RACSignal *)rac_didScroll {
    
//    RACSignal *willMoveSignal = [[self.rac_delegateProxy
//                                  signalForSelector:@selector(scrollViewWillBeginDragging:)]
//                                 merge:self.rac_willDeallocSignal];
    
//    RACSignal *changeSignal = [[[self.rac_delegateProxy signalForSelector:@selector(scrollViewDidZoom:)]
//                                reduceEach:^(UIScrollView *scrollView){
//        
//                                    return scrollView;
//                                }]
//                               setNameWithFormat:@""];
    RACSignal *didMoveSignal = [[[self.rac_delegateProxy
                                   signalForSelector:@selector(scrollViewDidScroll:)]
                                  reduceEach:^(UIScrollView *scrollView){
                                      
                                      return scrollView;
                                  }]
                                setNameWithFormat:@"%@ -rac_didScroll",self.rac_description];
    
    RACUseDelegateProxy(self);
    
    return didMoveSignal;
}

@end
